<?php
/**
 * $Id: SharedController.php 969 2008-07-29 13:55:14Z pawy $
 * Kontroller wspolny
 *
 */
class SharedController extends MainController implements ControllerInterface {
	/**
	 * domyslna metoda
	 *
	 */
	public function IndexAction($param) {
		
	}
		public function LeftPanel($param) {
		$this->smarty->assign('newComments', $this->CountNewComments($param));
		$this->smarty->assign('newCommentsToModerate', $this->CountNewCommentsToModerate($param));
		$this->smarty->assign('leftPanel', $this->smarty->fetch($this->templatePath.$this->partialTemplate));
	}
	public function Pagination($param) {

		if(isset($param['strona']) && $param['strona'] > 0 ){
			$page = $param['strona'];
			$URLparam = $this->GetUrlParam($param);
		}
		else{
			$page = 1;
			$URLtemp = $this->GetUrlParam($param);

			$URLparam = array($URLtemp[0], $URLtemp[1], 1, 'strona');

			$URLparam = array_merge($URLparam, array_slice($URLtemp, 2));
		}

		$this->smarty->assign('CurrentPage', $page);

		$URLParam = array("", "");

		$URLParam[0] = $URLparam[3];
		$URLParam[1] = $URLparam[1] . ',' . $URLparam[0];
		for ( $i = 4, $size = sizeof($URLparam) ; $i < $size ; $i++)
		{
			$URLParam[0] = $URLparam[$i] . ',' . $URLParam[0];
		}

		$this->smarty->assign("URLParam", $URLParam);
			
		$this->smarty->assign('pagination', $this->smarty->fetch($this->templatePath.$this->partialTemplate));

	}


	
	/**
	 * Startuje sesje, weryfikuje autoryzacje admina
	 *
	 */
	public function Auth() {
		session_start();
		$admin = AuthDAL::GetAdmin();
		$this->smarty->assign('admin', $admin);
		if(!is_object($admin)) {
			die(header("Location: ".Router::GenerateUrl('', array('login'=>'index'))));
			$this->AddRedirect("login", 0);
		} else {
                    $dalObj = ShopDAL::GetDalDataObj();
                    $dalObj->setId($admin->getIdShop());
                    $shopObj = ShopDAL::GetById($dalObj);
                    Registry::Set('shopObj', $shopObj);
                    $this->smarty->assign('shopObj', $shopObj);
                }
	}
	
	private function CountNewCommentsToModerate($param) {
		$dalObj = CommentsAbuseDAL::GetResult(
						CommentsAbuseDAL::GetDalDataObj(
								array(
									'count' => true,
									'condition' => array()
								)
						)
		);
		return $dalObj;
	}
	
	private function CountNewComments($param) {
		$dalObj = CommentsDAL::GetResult(
						CommentsDAL::GetDalDataObj(
								array(
									'count' => true,
									'condition' => array(
										'new' => array(1))
								)
						)
		);
		return $dalObj;
	}
	
	public function InfoFrame($param)
	{
		if(is_array($param))
		{
			$this->smarty->assign('error',$param["type"]);
			$this->smarty->assign('info',$param["info"]);
		}
		else
		{
			$this->smarty->assign('error',false);
			$this->smarty->assign('info',$param);
		}
		
	}
	
	public function userCanEquipProduct($param) {
		try {
			$userEq = new UserEquipment();
			$eq = $userEq->GetUserEq($this->user->getId(), $param['idProduct']);
			if(count($eq) > 0) {
				return false;
			} else {
				return  true;
			}
		} catch (Exception $e) {

		}
	}
	
	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {
	}
	/**
	 * 
	 *
	 */
	public function postDispatch($param) {
	}
}
